package com.CityHunter.Request;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.util.Log;

import com.CityHunter.Common.Http;
import com.CityHunter.DataObject.Buddy;

public class GetBuddyRequest {

	private String getBuddyPage;
	
	public GetBuddyRequest(String getBuddyPage){
		this.getBuddyPage = getBuddyPage;
	}
	
	public ArrayList<Buddy> sendRequest(int userid){

		ArrayList<Buddy> buddies;

		// Declare NameValuePair to store the data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userid", Integer.toString(userid)));

		// Send Get Buddy Request
		Http http = new Http();
		buddies = new ArrayList<Buddy>();
		Log.d("bdyp_Debug", "Sending Request : " + getBuddyPage);
		JSONArray jsonResponse = http.sendRequest(getBuddyPage, params);
		try {
			for (int i = 0; i < jsonResponse.length(); i++) {

				int buddyid = Integer.parseInt(jsonResponse.getJSONObject(i)
						.getString("buddyid"));
				String name = jsonResponse.getJSONObject(i).getString("name");
				int state = Integer.parseInt(jsonResponse.getJSONObject(i)
						.getString("state"));

				Buddy bb = new Buddy(buddyid, name, state);
				buddies.add(bb);
			}
			Log.d("bdyp_Debug",
					"Response : No of Buddy = " + jsonResponse.length());
		} catch (Exception e) {
			Log.e("bdyp_Error", e.getMessage().toString());
		}
		
		return buddies;
	}
	
}
